﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace badminton.Controls
{
    public partial class SearchPanels : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BadmintonEntities bde = new BadmintonEntities();
                this.DropDownListCityArea.Items.Add(new ListItem("不限", "-1"));
                this.DropDownListTraingCityArea.Items.Add(new ListItem("不限", "-1"));
                foreach (CityArea ca in bde.CityArea)
                {
                    this.DropDownListCityArea.Items.Add(new ListItem(ca.name, ca.id.ToString()));
                    this.DropDownListTraingCityArea.Items.Add(new ListItem(ca.name, ca.id.ToString()));
                }

                bindHallDropDown(-1);
                bindTrainingHallDropDown(-1);

                this.DropDownListTrainingCategory.Items.Add(new ListItem("不限", "-1"));
                foreach (TrainingCategory tc in bde.TrainingCategory)
                {
                    this.DropDownListTrainingCategory.Items.Add(new ListItem(tc.name, tc.id.ToString()));
                }

                this.TextBoxStartTime.Text = DateTime.Now.ToString("yyyy.MM.dd hh:mm");
            }

        }

        protected void bindHallDropDown(int cityareaid)
        {
            this.DropDownListHall.Items.Clear();
            this.DropDownListHall.Items.Add(new ListItem("不限", "-1"));
            if (cityareaid >= 0)
            {
                BadmintonEntities bde = new BadmintonEntities();
                foreach (Hall ha in bde.Hall.Where(p=>p.cityAreaId==cityareaid))
                {
                    this.DropDownListHall.Items.Add(new ListItem(ha.name, ha.id.ToString()));
                }
            }
        }

        protected void bindTrainingHallDropDown(int cityareaid)
        {
            this.DropDownListTraingHall.Items.Clear();
            this.DropDownListTraingHall.Items.Add(new ListItem("不限", "-1"));
            if (cityareaid >= 0)
            {
                BadmintonEntities bde = new BadmintonEntities();
                foreach (Hall ha in bde.Hall.Where(p => p.cityAreaId == cityareaid))
                {
                    this.DropDownListTraingHall.Items.Add(new ListItem(ha.name, ha.id.ToString()));
                }
            }
        }
        protected void OnCityAreaSelectedIndexChanged(object sender, EventArgs e)
        {
            this.Tabs.ActiveTabIndex = 0;
            int id = Convert.ToInt32(this.DropDownListCityArea.SelectedValue);
            bindHallDropDown(id);
        }


        protected void OnSearchTraining(object sender, CommandEventArgs e)
        {
            TrainingSearchSession trainingSearch = new TrainingSearchSession();
            trainingSearch.m_cityAreaId = Convert.ToInt32(this.DropDownListTraingCityArea.SelectedValue);
            trainingSearch.m_hallId = Convert.ToInt32(this.DropDownListTraingHall.SelectedValue);
            trainingSearch.m_trainingCatId = Convert.ToInt32(this.DropDownListTrainingCategory.SelectedValue);
            trainingSearch.m_searchStr = this.TextBoxTraingText.Text;

            Session["TrainingSearch"] = trainingSearch;

            Response.Redirect("~/pages/trainingsearchresult.aspx");

        }

        protected void OnSearchCourt(object sender, CommandEventArgs e)
        {
            DateTime dt = DateTime.Parse(this.TextBoxStartTime.Text).Date;
            CourtSearchSession courtSearch = new CourtSearchSession();
            courtSearch.m_startTime = dt;
            courtSearch.m_cityAreaId = Convert.ToInt32(this.DropDownListCityArea.SelectedValue);
            courtSearch.m_hallId = Convert.ToInt32(this.DropDownListHall.SelectedValue);
            courtSearch.m_timeSpan = Convert.ToDouble(this.TextBoxTimeSpan.Text);

            Session["CourtSearch"] = courtSearch;

            Response.Redirect("~/pages/hallsearchresult.aspx");
        }

        protected void OnTraingCityAreaChanged(object sender, EventArgs e)
        {
            this.Tabs.ActiveTabIndex = 1;
            int id = Convert.ToInt32(this.DropDownListTraingCityArea.SelectedValue);
            bindTrainingHallDropDown(id);

        }

    }
}